import {
  getSetting,
  openSetting,
  chooseAddress,
} from '../../util/asyncWx.js'
import regeneratorRuntime from '../../lib/runtime/runtime';
// pages/user/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    //收藏个数
    collectsNum: 0,
    //浏览足迹的个数
    historyNum: 0
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取缓存中的个人信息
    const userInfo = wx.getStorageSync('userInfo');
    const collects = wx.getStorageSync('collects') || [];
    const history = wx.getStorageSync('history') || [];
    this.setData({
      userInfo,
      collectsNum: collects.length,
      historyNum: history.length
    })
  },
  onPullDownRefresh() {
    wx.showLoading({
      title: '加载中',
      mask: true,
      direction: 1200
    });
    //获取缓存中的用户信息
    const userInfo = wx.getStorageSync('userInfo');
    //清空data中的数据
    this.setData({
      userInfo: {}
    })
    setTimeout(() => {
      wx.stopPullDownRefresh()
      wx.hideLoading();
      this.setData({
        userInfo
      })
    }, 1200)
  },
  //点击修改地址触发
  async mangerAddress() {
    try {
      //获取权限状态
      const res1 = await getSetting();
      const scope = res1.authSetting["scope.address"]
      if (scope === false) {
        //用户点击了取消，scope为false，要诱导用户重新赋予获取地址权限
        await openSetting()
      }
      //用户点击了确定或者是用户第一次点击获取地址api
      //获取地址信息
      let address = await chooseAddress()
      address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo
      console.log(address)
      wx.setStorageSync('address', address);
    } catch (error) {
      console.log(error)
    }
  },
})